package cn.net.mugui.net.p2p;

import java.net.DatagramSocket;

import org.bouncycastle.util.encoders.Hex;

import com.mugui.base.base.Autowired;
import com.mugui.base.base.Component;
import com.mugui.base.client.net.baghandle.NetHandle;
import com.mugui.base.client.net.bagsend.NetCall;
import com.mugui.base.client.net.bagsend.NetCall.Call;
import com.mugui.base.client.net.bean.NetBag;
import com.mugui.base.util.Other;

import cn.net.mugui.net.key.utils.Base58;
import cn.net.mugui.net.udp.UdpBag;
import cn.net.mugui.net.udp.UdpServer;

@Component
public class DefaultFuncHandle {

	@Autowired
	private NetHandle netHandle;

	@Autowired
	private ServerManager manager;

	@Autowired
	private Server server;

	public void doP2P(String string, DatagramSocket socket, UdpServer updbServer, UdpBag resolve, Call call) {
		System.out.println(string + "->" + updbServer.getTo_host() + " " + updbServer.getTo_port() + " ");
		ServerAttr serverAttr = manager.get(Base58.encode(resolve.getFrom()));
		try {
			NetBag bag = new NetBag();
			bag.setFrom_host(updbServer.getTo_host());
			bag.setFrom_port(updbServer.getTo_port());
			bag.setHost(socket.getLocalAddress().getHostAddress());
			bag.setPort(socket.getPort());
			bag.setSession(Base58.encode(resolve.getFrom()));
			bag.setTimestamp(resolve.getTime() + "");
			if (string.startsWith("ping") || string.startsWith("retPing")) {
				bag.setFunc(string);
				bag.setHash(Other.MD5(bag.toString()));
				NetHandle.netBagLocal.set(bag);
				NetBag resolveNetBag = netHandle.resolveNetBag(bag, null);
				NetHandle.netBagLocal.remove();
//				System.out.println(resolveNetBag.toString());
//				serverAttr.setPing(System.currentTimeMillis())
				return;
			} else if (string.startsWith("sucess")) {
				String string2 = new String(resolve.getData());
				NetBag newBean = null;
				if (string2.startsWith("{") && string2.endsWith("}")) {
					newBean = NetBag.newBean(NetBag.class, string2);
				}
				if (newBean != null) {
					bag.setFunc(newBean.getFunc());
					bag.setData(newBean.getData());
					bag.setHash(newBean.getHash());
				} else {
					bag.setFunc("sucess.method.base");
					bag.setData(string2);
				}
				if (bag.getHash() == null)
					bag.setHash(Hex.toHexString(resolve.getBag_hash()));
				if (bag.getFunc() != null && bag.getFunc().startsWith("ret.")) {
					NetCall netCall = netCallP2PManager.get(bag.getHash());
					if (netCall != null) {
						NetHandle.netBagLocal.set(bag);
						NetBag resolveNetBag = netHandle.resolveNetBag(bag, netCall);
						NetHandle.netBagLocal.remove();
						System.out.println(resolveNetBag.toString());
					}
					return;
				}
				NetHandle.netBagLocal.set(bag);
				NetBag resolveNetBag = netHandle.resolveNetBag(bag, null);
				NetHandle.netBagLocal.remove();
				System.out.println(resolveNetBag.toString());
				resolveNetBag.setFunc("ret." + resolveNetBag.getFunc());
				server.send(socket, updbServer, serverAttr, resolveNetBag.toString());
				return;

			}
			NetBag tempbag = netHandle.resolveNetBag(bag, null);
			if (tempbag == null)
				return;
			server.send(socket, updbServer, serverAttr, tempbag.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Autowired
	private NetCallP2PManager netCallP2PManager;

}
